package com.tcc.task.personal;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.tcc.login.logic.Login;

public class CategoryStatus {
	WebDriver driver = new FirefoxDriver();  
	@BeforeTest
	public void login() throws InterruptedException{
		driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
		Login myLogin = new Login();  
		myLogin.testLogin(driver);    
	}    
	@BeforeMethod
	public void beforeTestMethod(){
		System.out.println("Before Test Method");
	}
	//******************************************************************************************
	@Test(description="Add status",priority=1)
	public void add_Status(){
	driver.get("http://prod.campuscruiser.com/PageServlet?cx=u&pg=papp&tg=Tasks-prefStatus");
		for(int i=1;i<=3;i++){
	driver.findElement(By.xpath("//button[@type='BUTTON']")).click();
	driver.switchTo().alert().sendKeys(Integer.toString(i));
	driver.switchTo().alert().accept();
		}
		AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='statuses']/option[3]")).getText(),"1");
		AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='statuses']/option[4]")).getText(),"2");
		AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='statuses']/option[5]")).getText(),"3");
		driver.findElement(By.xpath("//input[@value='Save']")).click(); 
		Assert.assertEquals(driver.findElement(By.xpath("//div[@id='ccTgArea']/div[2]"))
				.getText(), "Status list has been updated.");
		
	}
	//********************************************************************************************
	@Test(description="Check Sort status function",priority=2)
	public void sort_Status(){
	driver.findElement(By.xpath("//select[@id='statuses']/option[5]")).click(); 
	for(int i=0;i<4;i++)
	{
	driver.findElement(By.xpath("//button[@type='button']")).click();
	}
	driver.findElement(By.xpath("//select[@id='statuses']/option[2]")).click(); 
	for(int i=0;i<3;i++)
	{
	driver.findElement(By.xpath("//button[@type='button'][2]")).click();
	}
	System.out.println("Add status OK!");
	AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='statuses']/option[1]")).getText(),"Completed");
	AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='statuses']/option[2]")).getText(),"1");
	AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='statuses']/option[3]")).getText(),"2");
	AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='statuses']/option[4]")).getText(),"3");
	AssertJUnit.assertEquals(driver.findElement(By.xpath("//select[@id='statuses']/option[5]")).getText(),"New");
	System.out.println("Compare status OK!");
	}

	//********************************************************************************************
	@Test(priority=4,description="Verify Edit status--new and complete")
	public void edit_NewComplete(){
		try{
	driver.findElement(By.xpath("//select[@id='statuses']/option[@value='0']")).click(); 
	driver.findElement(By.xpath("//button[@type='BUTTON'][2]")).click();
	AssertJUnit.assertEquals(driver.switchTo().alert().getText(),"Sorry, 'New' and 'Completed' are system reserved statuses.");
	driver.switchTo().alert().accept();
	driver.findElement(By.xpath("//select[@id='statuses']/option[@value='1']")).click(); 
	driver.findElement(By.xpath("//button[@type='BUTTON'][2]")).click();
	AssertJUnit.assertEquals(driver.switchTo().alert().getText(),"Sorry, 'New' and 'Completed' are system reserved statuses.");
	driver.switchTo().alert().accept();
	
	System.out.println("Edit test OK!");
		}
		catch(Exception e)
		{
			e.getMessage();
		}
	/*	if(driver.switchTo().alert().getText().equals("rry, 'New' and 'Completed' are system reserved statuses."))
		{
			System.out.println("Edit test OK!");
		}
		else
			
		{
			System.out.println("Edit test not OK!");
		}
		driver.switchTo().alert().accept();
	}
	*/
	}
	
	//*******************************************************************************************
		@Test(priority=4,description="Verify Edit status--new and complete")
		public void delete_NewComplete(){
			
		driver.findElement(By.xpath("//select[@id='statuses']/option[@value='0']")).click(); 
		driver.findElement(By.xpath("//button[@type='BUTTON'][2]")).click();
		AssertJUnit.assertEquals(driver.switchTo().alert().getText(),"Sorry, 'New' and 'Completed' are system reserved statuses.");
		driver.switchTo().alert().accept();
		driver.findElement(By.xpath("//select[@id='statuses']/option[@value='1']")).click(); 
		driver.findElement(By.xpath("//button[@type='BUTTON'][2]")).click();
		AssertJUnit.assertEquals(driver.switchTo().alert().getText(),"Sorry, 'New' and 'Completed' are system reserved statuses.");
		driver.switchTo().alert().accept();
			}
	//*******************************************************************************************
	@Test(description="Delete Customized Status",priority=5)
	public void delete_Status(){
	for(int i=0;i<3;i++)
	{
	driver.findElement(By.xpath("//select[@id='statuses']/option[2]")).click(); 
	driver.findElement(By.xpath("//button[@type='BUTTON'][3]")).click();
	}
	System.out.println("Delete test OK!");
	driver.findElement(By.xpath("//input[@value='Save']")).click(); 
	AssertJUnit.assertEquals(driver.findElement(By.xpath("//div[@id='ccTgArea']/div[2]")).getText(),"Status list has been updated.");
	}

}
